package com.umeng.analytics.social;

import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.pro.bv;
import com.umeng.analytics.pro.by;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public abstract class e {
    private static Map a;

    public e() {

    }

    protected static String[] a(Context arg6, String arg7, UMPlatformData[] arg8) throws a {
        if (arg8 != null && arg8.length != 0) {
            String v0 = AnalyticsConfig.getAppkey(arg6);
            if (TextUtils.isEmpty(((CharSequence) v0))) {
                throw new a("can`t get appkey.");
            } else {
                ArrayList v2 = new ArrayList();
                String v3 = "http://log.umsns.com/share/api/" + v0 + "/";
                if (e.a == null || (e.a.isEmpty())) {
                    e.a = e.b(arg6);
                }

                if (e.a != null && !e.a.isEmpty()) {
                    Iterator v4 = e.a.entrySet().iterator();
                    while (v4.hasNext()) {
                        Object v0_1 = v4.next();
                        ((List) v2).add(((Map.Entry) v0_1).getKey() + "=" + ((Map.Entry) v0_1).getValue());
                    }
                }

                ((List) v2).add("date=" + String.valueOf(System.currentTimeMillis()));
                ((List) v2).add("channel=" + d.d);
                if (!TextUtils.isEmpty(((CharSequence) arg7))) {
                    ((List) v2).add("topic=" + arg7);
                }

                ((List) v2).addAll(e.a(arg8));
                v0 = e.b(arg8);
                if (v0 == null) {
                    v0 = "null";
                }

                String v1;
                for (v1 = v3 + "?" + e.a(((List) v2)); v1.contains("%2C+"); v1 = v1.replace("%2C+", "&")) {
                }

                while (v1.contains("%3D")) {
                    v1 = v1.replace("%3D", "=");
                }

                while (v1.contains("%5B")) {
                    v1 = v1.replace("%5B", "");
                }

                while (v1.contains("%5D")) {
                    v1 = v1.replace("%5D", "");
                }

                by.b("url:" + v1 + "\nBody:" + v0);
                return new String[]{v1, v0};
            }
        }

        throw new a("platform data is null");
    }

    private static String a(List arg2) {
        String v0_2;
        try {
            ByteArrayOutputStream v0_1 = new ByteArrayOutputStream();
            v0_1.write(URLEncoder.encode(arg2.toString()).getBytes());
            v0_2 = v0_1.toString();
        } catch (Exception v0) {
            v0.printStackTrace();
            v0_2 = null;
        }

        return v0_2;
    }

    private static List a(UMPlatformData[] arg8) {
        char v7 = ',';
        StringBuilder v1 = new StringBuilder();
        StringBuilder v2 = new StringBuilder();
        StringBuilder v3 = new StringBuilder();
        int v4 = arg8.length;
        int v0;
        for (v0 = 0; v0 < v4; ++v0) {
            UMPlatformData v5 = arg8[v0];
            v1.append(v5.getMeida().toString());
            v1.append(v7);
            v2.append(v5.getUsid());
            v2.append(v7);
            v3.append(v5.getWeiboId());
            v3.append(v7);
        }

        if (arg8.length > 0) {
            v1.deleteCharAt(v1.length() - 1);
            v2.deleteCharAt(v2.length() - 1);
            v3.deleteCharAt(v3.length() - 1);
        }

        ArrayList v0_1 = new ArrayList();
        ((List) v0_1).add("platform=" + v1.toString());
        ((List) v0_1).add("usid=" + v2.toString());
        if (v3.length() > 0) {
            ((List) v0_1).add("weiboid=" + v3.toString());
        }

        return ((List) v0_1);
    }

    @SuppressLint("MissingPermission")
    public static Map a(Context arg5) {
        CharSequence v0_2;
        HashMap v2 = new HashMap();
        Object v0 = arg5.getSystemService("phone");
        if (v0 == null) {
            by.b("No IMEI.");
        }

        CharSequence v1 = null;
        try {
            if (!bv.a(arg5, "android.permission.READ_PHONE_STATE")) {
                v0_2 = v1;
            }else {
                v0_2 = ((TelephonyManager) v0).getDeviceId();
            }
        } catch (Exception v0_1) {
            by.e(((Throwable) v0_1));
            v0_2 = v1;
        }
        String v1_1 = bv.q(arg5);
        String v3 = Settings.Secure.getString(arg5.getContentResolver(), "android_id");
        if (!TextUtils.isEmpty(((CharSequence) v1_1))) {
            ((Map) v2).put("mac", v1_1);
        }

        if (!TextUtils.isEmpty(v0_2)) {
            ((Map) v2).put("imei", v0_2);
        }

        if (!TextUtils.isEmpty(v3)) {
            ((Map) v2).put("android_id", v3);
        }
        return v2;
    }

    private static Map b(Context arg7) throws a {
        HashMap v2 = new HashMap();
        Map v0 = e.a(arg7);
        if (v0 != null && !v0.isEmpty()) {
            StringBuilder v3 = new StringBuilder();
            StringBuilder v4 = new StringBuilder();
            Iterator v5 = v0.entrySet().iterator();
            while (v5.hasNext()) {
                Object v0_1 = v5.next();
                if (TextUtils.isEmpty((CharSequence) ((Entry) v0_1).getValue())) {
                    continue;
                }

                v4.append(((Map.Entry) v0_1).getKey()).append(",");
                v3.append(((Map.Entry) v0_1).getValue()).append(",");
            }

            if (v3.length() > 0) {
                v3.deleteCharAt(v3.length() - 1);
                ((Map) v2).put("deviceid", v3.toString());
            }

            if (v4.length() > 0) {
                v4.deleteCharAt(v4.length() - 1);
                ((Map) v2).put("idtype", v4.toString());
            }

            return ((Map) v2);
        }

        throw new a("can`t get device id.");
    }

    private static String b(UMPlatformData[] arg8) {
        JSONObject v2 = new JSONObject();
        int v3 = arg8.length;
        int v1 = 0;
        while (v1 < v3) {
            try {
                UMPlatformData v4 = arg8[v1];
                UMPlatformData.GENDER v0 = v4.getGender();
                String v5 = v4.getName();
                if (v0 == null) {
                    if (!TextUtils.isEmpty(((CharSequence) v5))) {
                        JSONObject v6 = new JSONObject();
                        String v7 = "gender";
                        String v0_2 = v0 == null ? "" : String.valueOf(v0.value);
                        v6.put(v7, v0_2);
                        v7 = "name";
                        v0_2 = v5 == null ? "" : v5;
                        v6.put(v7, v0_2);
                        v2.put(v4.getMeida().toString(), v6);
                    }
                    ++v1;
                    continue;

                } else {
                    JSONObject v6 = new JSONObject();
                    String v7 = "gender";
                    String v0_2 = v0 == null ? "" : String.valueOf(v0.value);
                    v6.put(v7, v0_2);
                    v7 = "name";
                    v0_2 = v5 == null ? "" : v5;
                    v6.put(v7, v0_2);
                    v2.put(v4.getMeida().toString(), v6);
                }
                ++v1;
                continue;
            } catch (Exception v0_1) {
                throw new a("build body exception", ((Throwable) v0_1));
            }
        }

        return v2.length() == 0 ? null : v2.toString();
    }
}

